﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiseLinkWPF.Windows.Controls.GridView;
using WiseLinkWPF.Windows.Controls;
using CCF20Model;
using System.Collections;
using System.Data;
namespace CCF20
{
    /// <summary>
    /// Interaction logic for VehicleMessage.xaml
    /// </summary>
    public partial class DeviceReturn : UserControl
    {
        VehicleModel vehicleModel;
        FaultModel faultModel;
        CCFZIC zic;
        
        public static readonly DependencyProperty FilterNameProperty = DependencyProperty.Register("FilterName", typeof(string), typeof(VehicleMessage), null);
        public string FilterName
        {
            get
            {
                return (string)GetValue(FilterNameProperty);
            }

            set
            {
                SetValue(FilterNameProperty, value);
            }
        }
        public DeviceReturn()
        {
            InitializeComponent();
            vehicleModel = new VehicleModel();
            faultModel = new FaultModel();
            zic = new CCFZIC();
            
            
        }

        //private void RadmaintenanceCount_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    var row = this.radGridTroubleVehicle.ItemContainerGenerator.ContainerFromItem(this.radGridTroubleVehicle.SelectedItem) as GridViewRow;
        //    if (row != null)
        //    {
        //        if (row.DetailsVisibility == Visibility.Visible)
        //        {
        //            row.DetailsVisibility = Visibility.Collapsed;
        //        }
        //        else 
        //        {
        //            row.DetailsVisibility = Visibility.Visible;
        //        }
        //    }

        //}

        private void VehicleMessageToolBar_Click(object sender, RoutedEventArgs e)
        {
            string strAction = ((Button)sender).Name;
            if (strAction == "GetNew")
            {
                //zic.TestGetDeviceRecord();
                return;

            }else if (strAction == "Refresh")
            {
                this.gridVehicle.Rebind();
                return;
            }
            else if (this.gridVehicle.SelectedItem == null)
            {
                MessageBox.Show("请先选择一条记录");
                return;
            }
            Vehicle currentVehicle = ((VehicleData)this.gridVehicle.SelectedItem).MyVehicle;

            
            switch (strAction)
            {
                case "Contact":
                    ContactCustomer contactCustomer = new ContactCustomer(vehicleModel, currentVehicle);
                    contactCustomer.Show();    
                    //contactCustomer.ShowDialog();
                    break;
                case "GiveUp":
                    //vehicleModel.GiveUpCustoemr(currentServiceRequest);
                    break;
                case "Return":
                    
                    break;
                case "Refresh":
                    //this.gridVehicle.ItemsSource = vehicleModel.GetAllVehicles();    
                    
                    //this.gridVehicle.Items.Refresh();
                    break;
            }
        }
        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.gridVehicle.ItemsSource = vehicleModel.GetAllVehicles(true);
            

            if (this.FilterName == "Hello World")
            {
                
            }
        }

        private void gridVehicle_SelectionChanged(object sender, SelectionChangeEventArgs e)
        {
            if (e.AddedItems.FirstOrDefault() != null)
            {
                this.gridVehicle.CurrentItem = e.AddedItems.First();
                VehicleData vd = (VehicleData)this.gridVehicle.CurrentItem;
                this.gridFault.ItemsSource = vehicleModel.GetUnconfirmedFaultByVehicle(vd.MyVehicle.VehicleID);
                //this.gridMaintenance.ItemsSource = vehicleModel.GetVehicleMaintenanceRecordByVehicle(vd.MyVehicle.VehicleID,true);
                //this.gridContactHistory.ItemsSource = vd.MyContactHistories;
                
            }
            else
            {
                this.gridFault.ItemsSource = null;
                this.gridMaintenance.ItemsSource = null;
                //this.gridContactHistory.ItemsSource = null;
                
            }
            this.Dispatcher.BeginInvoke(new Action(() => this.gridFault.Rebind()));
            this.Dispatcher.BeginInvoke(new Action(() => this.gridMaintenance.Rebind()));
            //this.Dispatcher.BeginInvoke(new Action(() => this.gridContactHistory.Rebind()));
         
        }

        private void RadMenuItem_Click(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
            GridViewRow row = ((ContextMenu)sender).PlacementTarget as GridViewRow;
            if (row != null)
            {
                row.IsSelected = true;
            }
            string temp = row.Cells[0].Content.ToString();
        }

        private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu) sender;
            GridViewRow row = menu.GetClickedElement<GridViewRow>();

            if (row != null)
            {
                row.IsSelected = row.IsCurrent = true;
                
            }
            else
            {
                menu.IsOpen = false;
            }
        }
        

        private void RadContextMenu_ItemClick(object sender, WiseLinkWPF.Windows.RadRoutedEventArgs e)
        {
            RadContextMenu menu = (RadContextMenu)sender;
            RadMenuItem clickedItem = e.OriginalSource as RadMenuItem;
            GridViewRow row = menu.GetClickedElement<GridViewRow>();
            
            Fault currentFault =  row.Item as Fault;
            if (clickedItem != null && row != null)
            {
                string header = Convert.ToString(clickedItem.Header);

                switch (header)
                {
                    case "确认":
                        faultModel.ConfirmTroubleCode(currentFault);
                        break;
                    case "修改":
                        this.gridFault.BeginEdit();
                        break;
                    case "排除":
                        faultModel.AddSkipTroubleCode(currentFault);
                        ((IList)this.gridFault.ItemsSource).Remove(row.DataContext);
                        //((ObservableCollection<Fault>)this.gridFault.ItemsSource).Remove((Fault)row.DataContext);
                        //this.gridFault.Items.Remove((Fault)row.DataContext);
                        break;
                    default:
                        break;
                }
            }
        }

        private void gridFault_RowEditEnded(object sender, GridViewRowEditEndedEventArgs e)
        {
            if (e.EditAction == GridViewEditAction.Cancel)
            {
                return;
            }
            else if (e.EditAction == GridViewEditAction.Commit)
            {
                vehicleModel.Update();
                return;
            }

            if (e.EditOperationType == GridViewEditOperationType.Insert)
            {
                
            }
            else if (e.EditOperationType == GridViewEditOperationType.Edit)
            {
            
            }

        }

        
    }
}
